package com.jeesite.modules.supplier.web;


import com.jeesite.common.utils.OrgUtil;
import com.jeesite.modules.supplier.entity.constant.SupplierConstant;
import com.jeesite.modules.supplier.entity.qo.SupplierQO;
import com.jeesite.modules.supplier.entity.vo.SupplierVO;
import com.jeesite.modules.supplierclass.entity.SupplierClass;
import com.jeesite.modules.supplierclass.service.SupplierClassService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.supplier.entity.Supplier;
import com.jeesite.modules.supplier.service.SupplierService;

/**
 * 供应商档案Controller
 * @author 码脑洞
 * @version 2025-04-13
 */
@Controller
@RequestMapping(value = "${adminPath}/supplier/supplier")
public class SupplierController extends BaseController {

	@Autowired
	private SupplierService supplierService;
	@Autowired
	private SupplierClassService supplierClassService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public Supplier get(Long autoId, boolean isNewRecord) {
		return supplierService.get(autoId, isNewRecord);
	}

	@RequiresPermissions("supplierclass:supplierClass:view")
	@RequestMapping(value = "index")
	public String index(SupplierClass supplierClass, Model model) {
		model.addAttribute("supplierClass", supplierClass);
		model.addAttribute("ctrlPermi", Global.getConfig("user.adminCtrlPermi", "2"));
		return "modules/supplier/supplierIndex";
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("supplier:supplier:view")
	@RequestMapping(value = {"list", ""})
	public String list(Supplier supplier, Model model) {
		model.addAttribute("supplier", supplier);
		model.addAttribute("supplierClassList", supplierClassService.selectSupplierClassList());
		return "modules/supplier/supplierList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("supplier:supplier:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<SupplierVO> listData(SupplierQO supplierQO, HttpServletRequest request, HttpServletResponse response) {
		supplierQO.setPage(new Page<>(request, response));
		Page<SupplierVO> page = supplierService.findSupplierPage(supplierQO);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("supplier:supplier:view")
	@RequestMapping(value = "form")
	public String form(Supplier supplier, Model model) {
		if (OrgUtil.isValid()){
			supplier.setOrgId(OrgUtil.getOrgId());
			supplier.setOrgCode(OrgUtil.getOrgCode());
			supplier.setOrgName(OrgUtil.getOrgName());
			supplier.setSourceType(SupplierConstant.SOURCE_TYPE_MANUAL);
		}
		model.addAttribute("supplierClassList", supplierClassService.selectSupplierClassList());
		model.addAttribute("supplier", supplier);
		return "modules/supplier/supplierForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("supplier:supplier:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated Supplier supplier) {
		supplierService.save(supplier);
		return renderResult(Global.TRUE, text("保存供应商档案成功！"));
	}
	
	/**
	 * 停用数据
	 */
	@RequiresPermissions("supplier:supplier:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(Supplier supplier) {
		supplier.setStatus(Supplier.STATUS_DISABLE);
		supplierService.updateStatus(supplier);
		return renderResult(Global.TRUE, text("停用供应商档案成功"));
	}
	
	/**
	 * 启用数据
	 */
	@RequiresPermissions("supplier:supplier:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(Supplier supplier) {
		supplier.setStatus(Supplier.STATUS_NORMAL);
		supplierService.updateStatus(supplier);
		return renderResult(Global.TRUE, text("启用供应商档案成功"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("supplier:supplier:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(Supplier supplier) {
		supplierService.delete(supplier);
		return renderResult(Global.TRUE, text("删除供应商档案成功！"));
	}
	
}